/**
 * Copyright (C) 2009 Ignasi Fosch Alonso
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * You can reach me by mail at natx@y10k.ws
 * You can reach him by paper mail at:
 * C. LLEIDA, 41-43, 3r 1a
 * El Prat de Llobregat 08820
 * Barcelona, SPAIN
 */
package ws.y10k.goloim.interfaces.swt;

import java.util.Enumeration;

import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import ws.y10k.goloim.model.Product;

/**
 * @author natx
 *
 */
public class AddProductWindow extends Window {
	
	Table tblProducts;
	private Label lblReference;
	Text txtReference;
	private Label lblDescription;
	Text txtDescription;
	private Label lblDetails;
	Text txtDetails;
	private Label lblSupplier;
	Text txtSupplier;
	private Label lblUnitNetWeight;
	Text txtUnitNetWeight;
	private Label lblUnitGrossWeight;
	Text txtUnitGrossWeight;
	private Label lblUnitsPerBox;
	Text txtUnitsPerBox;
	private Label lblBox;
	Combo cmbBox;
	private Label lblBoxesPerLayer;
	Text txtBoxesPerLayer;
	private Label lblLayersPerPallet;
	Text txtLayersPerPallet;
	private Label lblPallet;
	Combo cmbPallet;
	Button btnStackable;
	TableItem itmSelected = null;
	private Button btnSave;
	private Button btnCancel;

	public AddProductWindow(Window parent) {
		super(parent);
		this.setShellStyle(SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
		this.open();
	}
	
	public Control createContents(Composite parent) {
		// TODO i18n

		parent.getShell().setSize(920, 400);
		parent.getShell().setText("Add product");
		GoLoIm.center(parent.getShell());
		RowLayout lytRow = new RowLayout();
		parent.getShell().setLayout(lytRow);
		
		tblProducts = new Table(parent.getShell(),SWT.SINGLE | SWT.V_SCROLL);
		RowData rdt = new RowData();
		rdt.width = parent.getShell().getBounds().width - 30;
		rdt.height = parent.getShell().getBounds().height / 2;
		tblProducts.setLayoutData(rdt); 
		tblProducts.setLinesVisible(true);
		tblProducts.setHeaderVisible(true);
		String[] titles = {"Ref","Description","Details","Supplier","Net Wg","Gross Wg","Units\nBox","Box","Boxes\nLayer","Layers\nPallet","Pallet","Stackable"};
		for ( int i = 0; i < titles.length; i++ ) {
			TableColumn col = new TableColumn(tblProducts,SWT.NONE);
			col.setText(titles[i]);
		}
		Enumeration<Product> enuProducts = GoLoIm.products.enumeration();
		while ( enuProducts.hasMoreElements() ) {
			Product aProduct = enuProducts.nextElement();
			TableItem itmProduct = new TableItem(tblProducts,SWT.NONE);
			itmProduct.setText(0,aProduct.getReference());
			itmProduct.setText(1,aProduct.getDescription());
			itmProduct.setText(2,aProduct.getDetails());
			itmProduct.setText(3,aProduct.getSupplier());
			itmProduct.setText(4,((Double) aProduct.getUnitNetWeight()).toString());
			itmProduct.setText(5,((Double) aProduct.getUnitGrossWeight()).toString());
			itmProduct.setText(6,((Integer) aProduct.getUnitsPerBox()).toString());
			itmProduct.setText(7,aProduct.getBox().getWidth() + "x" + aProduct.getBox().getHeight() + "x" + aProduct.getBox().getDepth() + " " + aProduct.getBox().getWeight());
			itmProduct.setText(8,((Integer) aProduct.getBoxesPerLayer()).toString());
			itmProduct.setText(9,((Integer) aProduct.getLayersPerPallet()).toString());
			itmProduct.setText(10,aProduct.getPallet().getCode());
			String stackable = "N";
			if ( aProduct.isStackable() ) stackable = "Y";
			itmProduct.setText(11,stackable);
		}
		for ( int i = 0; i < titles.length; i++ ) {
			tblProducts.getColumn(i).pack();
		}
		tblProducts.addListener (SWT.Selection, new Listener () {
			@Override
			public void handleEvent (Event e) {
				TableItem [] selection = tblProducts.getSelection ();
				if ( itmSelected == null || !itmSelected.equals(selection[0]) ) {
					txtReference.setText(selection[0].getText(0));
					txtDescription.setText(selection[0].getText(1));
					txtDetails.setText(selection[0].getText(2));
					txtSupplier.setText(selection[0].getText(3));
					txtUnitNetWeight.setText(selection[0].getText(4));
					txtUnitGrossWeight.setText(selection[0].getText(5));
					txtUnitsPerBox.setText(selection[0].getText(6));
					cmbBox.select(cmbBox.indexOf(selection[0].getText(7)));
					txtBoxesPerLayer.setText(selection[0].getText(8));
					txtLayersPerPallet.setText(selection[0].getText(9));
					cmbPallet.select(cmbPallet.indexOf(selection[0].getText(10)));
					if ( selection[0].getText(11).equals("Y") ) btnStackable.setSelection(true);
					else btnStackable.setSelection(false);
					itmSelected = selection[0];
				} else {
					itmSelected = null;
					tblProducts.deselectAll();
					txtReference.setText("");
					txtDescription.setText("");
					txtDetails.setText("");
					txtSupplier.setText("");
					txtUnitNetWeight.setText("");
					txtUnitGrossWeight.setText("");
					txtUnitsPerBox.setText("");
					cmbBox.deselectAll();
					txtBoxesPerLayer.setText("");
					txtLayersPerPallet.setText("");
					cmbPallet.deselectAll();
					btnStackable.setSelection(false);
				}
			}
		});
		
		Composite cmpForm = new Composite(parent.getShell(), SWT.BORDER);
		rdt = new RowData();
		rdt.width = parent.getShell().getBounds().width - 30;
		rdt.height = parent.getShell().getBounds().height / 2;
		tblProducts.setLayoutData(rdt);
		GridLayout lytGridForm = new GridLayout();
		lytGridForm.numColumns = 8;
		cmpForm.setLayout(lytGridForm);
		

		lblReference = new Label(cmpForm,SWT.NONE);
		lblReference.setText("&Reference");
		GridData grdData = new GridData();
		grdData.widthHint = 100;
		lblReference.setLayoutData(grdData);
		txtReference = new Text(cmpForm,SWT.BORDER);
		grdData = new GridData();
		grdData.widthHint = 90;
		txtReference.setLayoutData(grdData);
		lblDescription = new Label(cmpForm,SWT.NONE);
		lblDescription.setText("&Description");
		grdData = new GridData();
		grdData.widthHint = 100;
		lblDescription.setLayoutData(grdData);
		txtDescription = new Text(cmpForm,SWT.BORDER);
		grdData = new GridData();
		grdData.widthHint = 90;
		txtDescription.setLayoutData(grdData);
		lblDetails = new Label(cmpForm,SWT.NONE);
		lblDetails.setText("D&etails");
		grdData = new GridData();
		grdData.widthHint = 100;
		lblDetails.setLayoutData(grdData);
		txtDetails = new Text(cmpForm,SWT.BORDER);
		grdData = new GridData();
		grdData.widthHint = 90;
		txtDetails.setLayoutData(grdData);
		lblSupplier = new Label(cmpForm,SWT.NONE);
		lblSupplier.setText("S&upplier");
		grdData = new GridData();
		grdData.widthHint = 100;
		lblSupplier.setLayoutData(grdData);
		txtSupplier = new Text(cmpForm,SWT.BORDER);
		grdData = new GridData();
		grdData.widthHint = 90;
		txtSupplier.setLayoutData(grdData);
		lblUnitNetWeight = new Label(cmpForm,SWT.NONE);
		lblUnitNetWeight.setText("Unit &Net Wg");
		grdData = new GridData();
		grdData.widthHint = 100;
		lblUnitNetWeight.setLayoutData(grdData);
		txtUnitNetWeight = new Text(cmpForm,SWT.BORDER);
		grdData = new GridData();
		grdData.widthHint = 30;
		txtUnitNetWeight.setLayoutData(grdData);
		lblUnitGrossWeight = new Label(cmpForm,SWT.NONE);
		lblUnitGrossWeight.setText("Unit &Gross Wg");
		grdData = new GridData();
		grdData.widthHint = 100;
		lblUnitGrossWeight.setLayoutData(grdData);
		txtUnitGrossWeight = new Text(cmpForm,SWT.BORDER);
		grdData = new GridData();
		grdData.widthHint = 30;
		txtUnitGrossWeight.setLayoutData(grdData);
		lblUnitsPerBox = new Label(cmpForm,SWT.NONE);
		lblUnitsPerBox.setText("Uni&ts/box");
		grdData = new GridData();
		grdData.widthHint = 100;
		lblUnitsPerBox.setLayoutData(grdData);
		txtUnitsPerBox = new Text(cmpForm,SWT.BORDER);
		grdData = new GridData();
		grdData.widthHint = 30;
		txtUnitsPerBox.setLayoutData(grdData);
		lblBox = new Label(cmpForm,SWT.NONE);
		lblBox.setText("&Box");
		grdData = new GridData();
		grdData.widthHint = 100;
		lblBox.setLayoutData(grdData);
		cmbBox = new Combo(cmpForm,SWT.READ_ONLY);
		cmbBox.setItems(GoLoIm.boxes.getStringArray());
		grdData = new GridData();
		grdData.widthHint = 130;
		cmbBox.setLayoutData(grdData);
		lblBoxesPerLayer = new Label(cmpForm,SWT.NONE);
		lblBoxesPerLayer.setText("Bo&xes/layer");
		grdData = new GridData();
		grdData.widthHint = 100;
		lblBoxesPerLayer.setLayoutData(grdData);
		txtBoxesPerLayer = new Text(cmpForm,SWT.BORDER);
		grdData = new GridData();
		grdData.widthHint = 30;
		txtBoxesPerLayer.setLayoutData(grdData);
		lblLayersPerPallet = new Label(cmpForm,SWT.NONE);
		lblLayersPerPallet.setText("&Layers/pallet");
		grdData = new GridData();
		grdData.widthHint = 100;
		lblLayersPerPallet.setLayoutData(grdData);
		txtLayersPerPallet = new Text(cmpForm,SWT.BORDER);
		grdData = new GridData();
		grdData.widthHint = 30;
		txtLayersPerPallet.setLayoutData(grdData);
		lblPallet = new Label(cmpForm,SWT.NONE);
		lblPallet.setText("&Pallet");
		grdData = new GridData();
		grdData.widthHint = 100;
		lblPallet.setLayoutData(grdData);
		cmbPallet = new Combo(cmpForm,SWT.READ_ONLY);
		cmbPallet.setItems(GoLoIm.pallets.getStringArray());
		grdData = new GridData();
		grdData.widthHint = 130;
		cmbPallet.setLayoutData(grdData);
		btnStackable = new Button(cmpForm,SWT.CHECK);
		btnStackable.setText("St&ackable");
		grdData = new GridData();
		grdData.widthHint = 200;
		grdData.horizontalSpan = 2;
		grdData.horizontalAlignment = SWT.CENTER;
		btnStackable.setLayoutData(grdData);
		btnSave = new Button(cmpForm, SWT.PUSH);
		grdData = new GridData();
		grdData.widthHint = 200;
		grdData.horizontalSpan = 4;
		grdData.horizontalAlignment = SWT.CENTER;
		btnSave.setLayoutData(grdData);
		btnSave.setText("&Save");
		btnSave.addSelectionListener(new SaveProductSelectionListener(this));
		btnCancel = new Button(cmpForm, SWT.PUSH);
		grdData = new GridData();
		grdData.widthHint = 200;
		grdData.horizontalSpan = 4;
		grdData.horizontalAlignment = SWT.CENTER;
		btnCancel.setLayoutData(grdData);
		btnCancel.setText("&Cancel");
		btnCancel.addSelectionListener(new CancelSelectionListener(parent.getShell()));
		txtReference.setFocus();
		parent.getShell().setDefaultButton(btnCancel);

		return parent;
		
	}

}
